//
//  HomeViewController.m
//  OneCreditTreasure
//
//  Created by 夏克镭 on 16/8/25.
//
//

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MDetailSettingViewController.h"//设置
#import "LoginViewController.h"
#import "NewCarLoaningViewController.h"
#import "CollectionFoldersViewController.h"
#import "MDMyCarViewController.h"

@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, MineHeaderViewDelegate, LoginViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) MineHeaderView *headerView;
@end

@implementation MineViewController
#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerSuccess:) name:@"register" object:nil];
}

- (void)registerSuccess:(NSNotification *)notification {
    [self checkMyLogin:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        self.navigationItem.title = @"壹贷宝";
        self.tabBarItem.title = @"我的";
        UIBarButtonItem *settings = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(goToSettingsPage:)];
        self.navigationItem.rightBarButtonItem = settings;
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"register" object:nil];
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? self.dataArr.count : 1;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        Cell.textLabel.text = self.dataArr[indexPath.row];
        Cell.textLabel.font = [UIFont systemFontOfSize:17 * AUTO_WIDTH];
    } else {
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Cell.textLabel.text = @"退出登陆";
        Cell.textLabel.textColor  = [UIColor redColor];
        Cell.textLabel.font = [UIFont systemFontOfSize:18 * AUTO_WIDTH];
        Cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return Cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        NewCarLoaningViewController *vc = [NewCarLoaningViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 0 && indexPath.row == 3) {
        MDMyCarViewController *vc = [MDMyCarViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 0 && indexPath.row == 5) {
        CollectionFoldersViewController *vc = [CollectionFoldersViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 1) {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Login"]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出登陆" message:@"您确定要退出登陆吗" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"Login"];
                _headerView = nil;
                self.tableView.tableHeaderView = self.headerView;
                [self.view showWarning:@"退出成功"];
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:cancelAction];
            [alert addAction:confirmAction];
            [self presentViewController:alert animated:YES completion:nil];
        } else {
            [self.view showWarning:@"您还未登陆"];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * AUTO_WIDTH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5 * AUTO_WIDTH;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

#pragma mark - MineHeaderView delegate
- (void)goToLogin:sender {
    LoginViewController *vc = [LoginViewController new];
    vc.delegate = self;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)checkMyLogin:sender {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Login"];
    _headerView = nil;
    self.tableView.tableHeaderView = self.headerView;
    [self.view showWarning:@"登陆成功"];
}

- (void)getMoney:sender {
    
}

- (void)recharge:sender {
    
}

#pragma mark - 方法 methods
- (void)goToSettingsPage:sender {
    MDetailSettingViewController *vc = [MDetailSettingViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}

- (NSArray *)dataArr {
    if(_dataArr == nil) {
        _dataArr = @[@"我的方案",
                     @"我的还款",
                     @"我的投资",
                     @"我的车辆",
                     @"我的银行卡",
                     @"收藏夹"];
    }
    return _dataArr;
}

- (MineHeaderView *)headerView {
    if(_headerView == nil) {
        _headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 200 * AUTO_WIDTH)];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.delegate = self;
    }
    return _headerView;
}

@end
